<?php

namespace App\Http\Service;

use App\Models\Viewcount;
use App\Models\Doubanpost;
use App\Http\Service\BaseService;
use App\Http\Service\ChatgptService;
use App\Http\Service\UserService;
use App\Http\Service\ViewcountmoreService;
use App\Models\Baoliaopostback;

class ViewcountService  extends BaseService
{
    private $model;
    public  $userService;
    public  $viewcountmoreService;
    public function __construct(Viewcount $model,ChatgptService $chatgptService,UserService  $userService,ViewcountmoreService $viewcountmoreService,Baoliaopostback $baoliaopostback)
    {
        parent::__construct();
        $this->model = $model;
        $this->chatgptService=$chatgptService;
        $this->userService=$userService;
        $this->viewcountmoreService=$viewcountmoreService;
        $this->baoliaopostback=$baoliaopostback;
       
    }


    public function getList($request,$data=[])
    {
        $page = intval($request->input('page')) ?$request->input('page') :$this->cpage;
        $limit = intval($request->input('limit')) ?$request->input('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $where = [];
        if(isset($data['today_time'])&&!empty($data['today_time'])){
            $where[]=['A.today_time','=',$data['today_time']];
        }
        if(isset($data['people_id'])&&!empty($data['people_id'])){
            $where[]=['A.people_id','=',$data['people_id']];
        }
        if(isset($data['title'])&&!empty($data['title'])){
            $where[]=['A.title','=',$data['title']];
        }
        //dd($data);
        
        
        $type=$data['type'];
        #豆瓣(逛逛)
/*        1  豆瓣
        2  爆料
        3  逛逛
        4  豆瓣买组
        5  豆瓣拼组*/
        if($type=='1'){
            $where[]=['A.type','=','1'];
        }
        if($type=='2'){
            $where[]=['A.type','=','2'];
            $where[]=['A.type_son','=','6'];
        }
        if($type=='3'){
            $where[]=['A.type','=','1'];
            $where[]=['A.type_son','=','5'];
        }

        if($type=='4'){
            $where[]=['A.type','=','1'];
            $where[]=['A.type_son','=','3'];
        }

        if($type=='5'){
            $where[]=['A.type','=','1'];
            $where[]=['A.type_son','=','4'];
        }



        if($type=='1'){
            //1  豆瓣
            $count = $this->model->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['A.*'];
            $list = $this->model->select($fields)
            ->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)
            ->offset($offset)->limit($limit)
            ->orderBy('A.update_time', 'desc')
            ->get();
            //dd($list);

        }else if($type=='2'){
            //爆料
            $count = $this->model->from('view_count as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['A.*','B.platform_name'];
            $list = $this->model->select($fields)
            ->from('view_count as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)
            ->offset($offset)->limit($limit)
            ->orderBy('A.update_time', 'desc')
            ->get();

        }else if($type=='3'){
            //逛逛
            //$this->getLastSql();
            $count = $this->model->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            //dd($count);
            $fields = ['A.*'];
            $list = $this->model->select($fields)
            ->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)
            ->offset($offset)->limit($limit)
            ->orderBy('A.update_time', 'desc')
            ->get();

        }else if($type=='4'){
            //豆瓣买组
            //$this->getLastSql();
            $count = $this->model->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['A.*'];
            $list = $this->model->select($fields)
            ->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)
            ->offset($offset)->limit($limit)
            ->orderBy('A.update_time', 'desc')
            ->get();

        }else if($type=='5'){
            //5  豆瓣拼组
           // $this->getLastSql();
            $count = $this->model->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['A.*','B.platform_name'];
            $list = $this->model->select($fields)
            ->from('view_count as A') // 主表使用别名
            ->join('douban_post as B', 'A.unique_id', '=', 'B.unique_id') // 关联表使用别名
            ->where($where)
            ->offset($offset)->limit($limit)
            ->orderBy('A.update_time', 'desc')
            ->get();
        }

        
        
        //dd($user);
        #查看昵称头像
        foreach ($list as $key => &$value) {
            $user_id=$value['user_id'];
            $user=$this->userService->update_user($user_id);
            $value['nickname']=$user['nickname'];
            $value['avatar']=$user['avatar'];
        }


        $result = [
            'page'=> $page,
            'limit'=> $limit,
            'total_page'=> ceil($count/$limit),
            'count'=>$count,
            'data'=> $list
        ];
        return $result; 

    }
    
    #添加浏览记录 
    public function addDate($request,$data=[])
    {
        $parameter=$data;
        unset($data['table_type']);
        //dd($data);
        $data['today_time']=date("Y-m-d");
        $user_id="0";
        if(isset($data['user_id'])){
            $user_id=$data['user_id'];
        }
        if(isset($data['people_id'])){
            $user_id=$data['people_id'];
        }

        $where=[
            'unique_id'=>$data['unique_id'],
            'people_id'=>$data['people_id'],
            'type'=>$data['type'],
            'type_son'=>$data['type_son'],
            'today_time'=>$data['today_time'],
            'user_id'=>$user_id,
        ];
        if(isset($data['unit_price'])&&empty($data['unit_price'])){
            $data['unit_price']=0.00;
        }
        if(isset($data['commission_amount'])&&empty($data['commission_amount'])){
            $data['commission_amount']=0.00;
        }
       // dd($data);
        $first=$this->model->where($where)->first();
        //dd($first);
        if(!$first){
            $data['create_time']=date("Y-m-d H:i:s");
            $data['update_time']=date("Y-m-d H:i:s");
           
            $insertId=$this->model->insertGetId($data);  
        }else{
            $data['update_time']=date("Y-m-d H:i:s");
            $this->model->where($where)->update($data);

        }
        #浏览记录添加更多
        $this->viewcountmoreService->addData($request,$parameter);
        return true;
        
        
    }
    //删除
    public function del($request,$data=[],$ids)
    {
        $res =  $this->model->whereIn('id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   
}
